home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH8 / SRC / BINTREE.FRM (.txt) next >
Encoding:
Visual Basic Form  |  1996-03-04  |  3.6 KB  |  121 lines

  1. VERSION 4.00
  2. Begin VB.Form BinTreeForm 
  3.    Caption         =   "Binary Tree"
  4.    ClientHeight    =   4140
  5.    ClientLeft      =   2445
  6.    ClientTop       =   1620
  7.    ClientWidth     =   4470
  8.    Height          =   4830
  9.    Left            =   2385
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   276
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   298
  14.    Top             =   990
  15.    Width           =   4590
  16.    Begin VB.PictureBox Canvas 
  17.       AutoRedraw      =   -1  'True
  18.       Height          =   3495
  19.       Left            =   0
  20.       ScaleHeight     =   229
  21.       ScaleMode       =   3  'Pixel
  22.       ScaleWidth      =   293
  23.       TabIndex        =   3
  24.       Top             =   0
  25.       Width           =   4455
  26.    End
  27.    Begin VB.CommandButton CmdGo 
  28.       Caption         =   "Go"
  29.       Default         =   -1  'True
  30.       Height          =   495
  31.       Left            =   1920
  32.       TabIndex        =   2
  33.       Top             =   3600
  34.       Width           =   735
  35.    End
  36.    Begin VB.TextBox LevelText 
  37.       Height          =   285
  38.       Left            =   480
  39.       MaxLength       =   3
  40.       TabIndex        =   1
  41.       Text            =   "5"
  42.       Top             =   3720
  43.       Width           =   375
  44.    End
  45.    Begin VB.Label Label1 
  46.       Caption         =   "Level"
  47.       Height          =   255
  48.       Left            =   0
  49.       TabIndex        =   0
  50.       Top             =   3720
  51.       Width           =   495
  52.    End
  53.    Begin VB.Menu mnuFile 
  54.       Caption         =   "&File"
  55.       Begin VB.Menu mnuFileExit 
  56.          Caption         =   "E&xit"
  57.       End
  58.    End
  59. Attribute VB_Name = "BinTreeForm"
  60. Attribute VB_Creatable = False
  61. Attribute VB_Exposed = False
  62. Option Explicit
  63. Const PI = 3.14159
  64. Const PI_2 = PI / 2
  65. Const PI_5 = PI / 5
  66. Const LENGTH_SCALE = 0.75
  67. Const DTHETA = PI_5
  68. Dim TheLevel As Integer
  69. Dim StartX As Integer
  70. Dim StartY As Integer
  71. Dim StartLength As Integer
  72. ' ************************************************
  73. ' Recursively draw a binary tree branch.
  74. ' ************************************************
  75. Sub DrawBranch(level As Integer, x As Integer, y As Integer, length As Integer, theta As Single)
  76. Dim x1 As Integer
  77. Dim y1 As Integer
  78.     ' See where this branch should end.
  79.     x1 = x + length * Cos(theta)
  80.     y1 = y + length * Sin(theta)
  81.     Canvas.Line (x, y)-(x1, y1)
  82.     ' If level > 1, draw the attached branches.
  83.     If level > 1 Then
  84.         DrawBranch level - 1, x1, y1, length * LENGTH_SCALE, theta + DTHETA
  85.         DrawBranch level - 1, x1, y1, length * LENGTH_SCALE, theta - DTHETA
  86.     End If
  87. End Sub
  88. Private Sub CmdGo_Click()
  89.     Canvas.Cls
  90.     MousePointer = vbHourglass
  91.     DoEvents
  92.     StartLength = (Canvas.ScaleHeight - 10) / _
  93.         ((1 - LENGTH_SCALE ^ (TheLevel + 1)) / (1 - LENGTH_SCALE))
  94.     DrawBranch TheLevel, StartX, StartY, StartLength, -PI_2
  95.     MousePointer = vbDefault
  96. End Sub
  97. Private Sub Form_Load()
  98.     TheLevel = CInt(LevelText.Text)
  99. End Sub
  100. Private Sub Form_Resize()
  101. Const GAP = 3
  102.     CmdGo.Move (ScaleWidth - CmdGo.Width) / 2, _
  103.         ScaleHeight - CmdGo.Height - GAP
  104.     Label1.Top = CmdGo.Top + (CmdGo.Height - Label1.Height) / 2
  105.     LevelText.Top = Label1.Top
  106.     Canvas.Move 0, 0, ScaleWidth, CmdGo.Top - GAP
  107.     StartX = Canvas.ScaleWidth \ 2
  108.     StartY = Canvas.ScaleHeight - 5
  109. End Sub
  110. Private Sub LevelText_Change()
  111.     If IsNumeric(LevelText.Text) Then
  112.         TheLevel = CInt(LevelText.Text)
  113.     Else
  114.         TheLevel = -1
  115.     End If
  116.     CmdGo.Enabled = TheLevel > 0
  117. End Sub
  118. Private Sub mnuFileExit_Click()
  119.     Unload Me
  120. End Sub
  121.